From 974c054bc97c312f13c81bafbc6c6f6d933088bd Mon Sep 17 00:00:00 2001 From: Marvin Kopf Date: Sat, 1 Apr 2017 13:57:51 +0200 Subject: fix endless loop in StringReplace (#3658) * Fixed a recursive loop where the replacement would again be searched for the needle. * Skip if the needle is empty. Find(needle) always matches if needle is empty. --- src/StringUtils.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/StringUtils.cpp b/src/StringUtils.cpp index e70c2dc50..4ff556277 100644 --- a/src/StringUtils.cpp +++ b/src/StringUtils.cpp @@ -374,11 +374,17 @@ size_t RateCompareString(const AString & s1, const AString & s2) void ReplaceString(AString & iHayStack, const AString & iNeedle, const AString & iReplaceWith) { + // find always returns the current position for an empty needle; prevent endless loop + if (iNeedle.empty()) + { + return; + } + size_t pos1 = iHayStack.find(iNeedle); while (pos1 != AString::npos) { iHayStack.replace( pos1, iNeedle.size(), iReplaceWith); - pos1 = iHayStack.find(iNeedle, pos1); + pos1 = iHayStack.find(iNeedle, pos1 + iReplaceWith.size()); } } -- cgit v1.2.3